package controllers

import (
	"day8/models"
	beego "github.com/beego/beego/v2/server/web"
)

type MvController struct {
	beego.Controller
}
type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *MvController) List() {
	var mv []models.Mv
	err := models.DB.Find(&mv).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *MvController) Add() {
	bt := c.GetString("bt")
	jj := c.GetString("jj")
	img := c.GetString("img")
	time := c.GetString("time")
	zz := c.GetString("zz")
	mv := models.Mv{
		Bt:   bt,
		Jj:   jj,
		Img:  img,
		Time: time,
		Zz:   zz,
	}
	err := models.DB.Create(&mv).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *MvController) Del() {
	id, _ := c.GetInt("img")
	err := models.DB.Delete(&models.Mv{}, id).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
